﻿@page "/account/resetpassword"
@page "/account/resetpassword/{UserId}"
@layout LoginLayout

@inject NavigationManager navigationManager
@inject HttpClient Http
@inject AuthenticationStateProvider authStateProvider
@inject IStringLocalizer<Global> L
@inject IMatToaster matToaster

<MatCard>
    <div class="logo">
        <img src="_content/BlazorBoilerplate.Theme.Material/images/logo.svg" style="width:100px;" /><br />BlazorBoilerplate
        <br />
        <h4>@L["Password Reset"]</h4>
    </div>
    <EditForm Model="@resetPasswordViewModel" OnValidSubmit="@SendResetPassword">
        <FluentValidationValidator />
        <ValidationSummary />
        <fieldset>
            <div class="form-group">
                <MatTextField @bind-Value="@resetPasswordViewModel.Password" Label=@L["Password"] Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true" Type="password"></MatTextField>
            </div>
            <div class="form-group">
                <MatTextField @bind-Value="@resetPasswordViewModel.PasswordConfirm" Label=@L["Password Confirmation"] Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true" Type="password"></MatTextField>
            </div>
            <div class="form-group d-flex justify-content-end">
                <MatButton Type="submit" Raised="true">@L["Reset Password"]</MatButton>
            </div>
        </fieldset>
        <hr />
        <NavLink href="/">
            <h6 class="font-weight-normal text-center">@L["Cancel"]</h6>
        </NavLink>
    </EditForm>
</MatCard>

@code {
    ResetPasswordViewModel resetPasswordViewModel { get; set; } = new ResetPasswordViewModel();

    [Parameter]
    public string UserId { get; set; }

    protected override void OnInitialized()
    {
        // Blazor does not have query string accessible parameters yet so a hack is needed. Token is too long for path parameter
        var absoluteUrl = navigationManager.Uri;
        var token = absoluteUrl.Substring(absoluteUrl.IndexOf("?token=") + 7);

        if (!string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(token))
        {
            resetPasswordViewModel.Token = token;
            resetPasswordViewModel.UserId = this.UserId;
        }
        else
        {
            matToaster.Add("Your url is missing the Reset Token. Fatal Error", MatToastType.Danger, "Reset Token is Missing");
        }
    }

    async Task SendResetPassword()
    {
        try
        {
            ApiResponseDto apiResponse = await ((IdentityAuthenticationStateProvider)authStateProvider).ResetPassword(resetPasswordViewModel);

            if (apiResponse.IsSuccessStatusCode)
            {
                matToaster.Add(L["ResetPasswordSuccessful"], MatToastType.Success);
                navigationManager.NavigateTo(BlazorBoilerplate.Constants.Settings.LoginPath);
            }
            else
            {
                matToaster.Add(apiResponse.Message, MatToastType.Danger, L["ResetPasswordFailed"]);
            }
        }
        catch (Exception ex)
        {
            matToaster.Add(ex.Message, MatToastType.Danger, L["ResetPasswordFailed"]);
        }
    }
}
